<?php
    namespace Home\Controller;
    use Think\Controller;
    class LoginController extends Controller {
        public static $logsta = "";
        public function index(){
            $this->display();

        }
        public function submit(){
            $username = I('post.log-cacct');
            $this->assign('usr',$username);
            $password = I('post.log-pwd');
            $_SESSION['usr'] = $username;
            $_SESSION['pwd'] = $password;
            $this->display();
            //采用登录与登录状态分离的方式，可以防止一些恶意操作。
        }
        public function getloginstatus(){
            $user = D('Usr');
            $condition['username'] = session('usr');
            $condition['password'] = session('pwd');
            $data = $user->where($condition)->find();
            $root = $user->where($condition)->getField('root');
            if ($root!=0){
                $_SESSION['root']=$root;
            }else{
                self::$logsta = 'rooterror';
            }
            if(!$data){
                self::$logsta = "pwderror";
            }else{
                self::$logsta = "OK";
                session('uid',$condition['username']);
                session('nickname',$data['nickname']);
                //清空临时储存
                session('usr',null);
                session('pwd',null);
            }               
            if(!empty(self::$logsta)){
                $this->ajaxReturn(self::$logsta);
            }else{
                $this->ajaxReturn("timeout");
            }

        }
        public function out(){
            session('uid',null);
            session('nickname',null);
            redirect(U('Login/index'),1,'<meta charset="utf-8">已成功退出登录,正在为你跳转！');
        }
    }
